/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metro.jme.lottery.business;

import java.util.Hashtable;
import metro.jme.lottery.persist.FavBean;

/**
 * This class is used as an interface between the screen (NumberCanvas) and 
 * XmlLoader. The UI will ask infos to this class, which will process them and 
 * give them to the UI.
 * @author pierre
 */
public class Checker {

	private int good;
	private int bonus;
	private Hashtable gainsTable;
	private int[] results;
	private int[] bonusResults;
	
	
	public Checker() throws Exception {
		int[] stored = FavBean.getFavSetAsArray();
		String page = XmlLoader.grabPage();
		if (page != null) {
			results = XmlLoader.getGood(page);
			bonusResults = XmlLoader.getBonus(page);


			gainsTable = XmlLoader.getGains(page);

			good = getGood(stored, results);
			bonus = getGood(stored, bonusResults);
			System.out.println("good = " + good);
			System.out.println("bonus = " + bonus);
		} else {
			throw new Exception("Error grabing page");
		}
	}

	public static boolean[] fullCheck(int[] stored, int[] results) {
		boolean[] check = new boolean[stored.length];

		if (stored.length <= results.length) {
			for (int i = 0; i < stored.length; i++) {
				check[i] = (stored[i] == results[i]);
			}
		}

		return check;
	}

	private static int getGood(int[] stored, int[] results) {
		int check = 0;

		boolean shouldContinue = true;

		// Test only if stored is bigger than results and if both arrays are not null
		if (stored != null && results != null) {
			//if (stored.length <= results.length) {
			int i = 0;
			while (i < stored.length) {
				//for (int i = 0; i < stored.length; i++) {
				// Now try to find the i value from stored in results
				int j = 0;
				shouldContinue = true;
				while (j < results.length && shouldContinue) {
					if (stored[i] == results[j]) {
						check++;
						shouldContinue = false;
					}
					j++;
				}
				//}
				i++;
			}
		}

		return check;
	}

	private static int getBonus(int[] stored, int[] results) {
		int bonus = 0;

		return bonus;
	}

	public float getGains() {
		float gains = (float) 0.0;
		String good = "" + this.good;
		String combo = "";
		if (bonus != 0) {
			combo = good + "+" + bonus;
		}
		boolean found = false;
		// First try to find gains if we have some extra numbers
		if (bonus != 0) {
			//FIXME: if the results contain 4, 4+1, 4+2, 4+1 will not get good results
			if (gainsTable.containsKey(combo)) {
				gains = Float.parseFloat((String) gainsTable.get(combo));
				found = true;
			}
			// As we have a bonus, maybe we didn't find the equals string (e.g 4.2)
			// So we have to check if "4" have a value.
			if (gainsTable.containsKey(good) && found == false) {
				// Cast the element for key "good" as a float, the we'll return it.
				gains = Float.parseFloat((String) gainsTable.get(good));
			}
		} else {
			if (gainsTable.containsKey(good)) {
				gains = Float.parseFloat((String) gainsTable.get(good));
			}
		}
		return gains;
	}

	public int getBonus() {
		return bonus;
	}

	public int getGood() {
		return good;
	}

	public int[] getBonusResults() {
		return bonusResults;
	}
	
	public String getBonusResultsAsString() {
		String str = "";
		int[] br =getBonusResults();
		str += br[0];
		for (int i = 1; i < br.length; i++) {
			str += ", "+br[i];
		}
		return str;
	}

	public int[] getResults() {
		return results;
	}
	
	public String getResultsAsString() {
		String str = "";
		int[] br =getResults();
		str += br[0];
		for (int i = 1; i < br.length; i++) {
			str += ", "+br[i];
		}
		return str;
	}
}
